layui.use(['form', 'table', 'upload', 'element'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        upload = layui.upload,
        element = layui.element;

        element.init(); //初始化element

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //点击选择相册按钮
    $("#photoAlbumBtn").on("click", function(){
        var photoAlbumId = $("#photoAlbumId").val();
        var photoAlbumName = $("#photoAlbumName").val();
        var content = layuimini.getHrefContent('/web/boPhoto/choosePhotoAlbum?photoAlbumId=' + photoAlbumId + "&photoAlbumName=" + photoAlbumName + "&type=1");
        var index = layer.open({
            title: '选择相册',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['80%','80%'], //宽高
            content: content
        });
        // $(window).on("resize", function () {
        //     layer.full(index);
        // });

        return true;
    });

    //点击保存上传视频
    var photoNum = 0; //上传视频总数
    var photoCount = 0; //已成功上传的视频数量
    var uploadInst = upload.render({
        elem: '#uploadPhotoBtn'
        ,url: '/web/boPhoto/uploadAlbumPhotoVideo' //改成您自己的上传接口
        ,multiple: true //多文件上传
        ,accept: 'video'
        ,number: 5 //一次只能上传5个视频
        ,acceptMime: 'video/mp4' //只显示视频文件
        ,auto: false //点击按钮才上传
        ,bindAction: '#saveBtn'
        ,data: { //额外参数
            photoAlbumId: function(){
                return $('#photoAlbumId').val();
            }
        }
        ,choose: function(obj){ //选中预览
            photoNum = 0; //初始化视频总数
            photoCount = 0; //初始化已成功上传的视频数量
            $('#pre_photo').empty(); //清空预览
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                photoNum++;//上传视频总数
                $('#pre_photo').append('<video controls style="max-width:150px; max-height:300px;margin: 5px;"><source src="'+ result +'" type="video/mp4">您的浏览器不支持视频。</video>');

                var percent = photoCount + '/' + photoNum;
                $("#photo-progress").attr('lay-percent', percent); //设置进度
                element.init(); //初始化element
            });

        }
        ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
            layer.load(2); //上传loading
        }
        ,done: function(res){ //
            //上传成功
            if(res.state == "200"){
                photoCount ++;
                var percent = photoCount + '/' + photoNum;
                // element.progress('photo-progress', percent); //设置进度
                $("#photo-progress").attr('lay-percent', percent); //设置进度
                element.init(); //初始化element
            }
        }
        ,allDone: function(obj){ //当文件全部被提交后，才触发
            layer.closeAll('loading'); //关闭加载层
            var text = '总视频数：' + obj.total + ' 个<br/>上传成功：' + obj.successful + ' 个<br/>上传失败：' + obj.aborted + ' 个';
            layer.open({
                content: text,
                yes: function(index, layero){
                    layer.close(index); //如果设定了yes回调，需进行手工关闭
                    layer.close(parentIndex); //关闭弹出层
                }
            });
        }
    });

    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

});